#!/bin/bash
trap 'echo Abort ; exit 1' ERR
err() { echo Error. $@ not found.; exit 1; }

if(test -L $0) then
    	auxlink=`ls -l $0 | sed 's/^[^>]*-> //g'`
    	M_HOME=`dirname $auxlink`/
else 
    	M_HOME=`dirname $0`/
fi
cd $M_HOME

echo Build Linux Kernel.

[ ! -d /source ] && err /source
[ ! -f download ] && err download
[ ! -f patch ] && err patch
[ ! -d /opt/9263/kernelmodules ] && err /opt/9263/kernelmodules
[ ! -f /opt/9263/deploy/mkimage ] && err /opt/9263/deploy/mkimage
[ ! -f ./modifyconfig ] && err ./modifyconfig

LINUX=linux-2.6.28.8

rm $LINUX -rf
[ ! -f /source/${LINUX}.tar.bz2 ] && wget -c --tries=20 http://www.cn.kernel.org/pub/linux/kernel/v2.6/${LINUX}.tar.bz2 && cp ${LINUX}.tar.bz2 /source/
cp /source/${LINUX}.tar.bz2 ./
tar xf ${LINUX}.tar.bz2
pushd ${LINUX}
patch -p1 < ../patch
ARG="ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi- INSTALL_MOD_PATH=/opt/9263/kernelmodules"
set -x
make $ARG at91sam9263ek_defconfig > /dev/null
../modifyconfig
make $ARG > /dev/null
make $ARG modules_install > /dev/null
set +x
/opt/9263/deploy/mkimage -A arm -O linux -T kernel -C none -a 0x20008000 -e 0x20008000 -n $LINUX -d arch/arm/boot/zImage /opt/9263/deploy/kernel.bin
popd
rm $LINUX.tar.{gz,bz2} -rf 
